package com.example.peibei.ui.activity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.dingtao.common.bean.Dynamic;
import com.dingtao.common.bean.RefreshResult;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDActivity;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.FileCacheUtils;
import com.dingtao.common.util.SPUtils;
import com.dingtao.common.util.UIUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.service.presenter.DeleteDynamicPresenter;
import com.example.peibei.service.presenter.DynamicPresenter;
import com.example.peibei.service.presenter.MyDynamicPresenter;
import com.example.peibei.ui.adapter.DynamicListAdapter;
import com.example.peibei.ui.fragment.DynamicListFragment;
import com.example.peibei.ui.weight.EmptyRecyclerView;
import com.linxiao.framework.dialog.BottomDialog;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MyDynamicActivity extends WDActivity {
    @BindView(R.id.rv_dynamic_list)
    EmptyRecyclerView rv_dynamic_list;
    @BindView(R.id.empty_view)
    LinearLayout empty_view;

    List<Dynamic.RecordsDTO> dynamics;
    private DynamicListAdapter dynamicListAdapter;
    private SPUtils spUtils;
    private String token;
    private MyDynamicPresenter dynamicPresenter;
    private long mid;
    private DeleteDynamicPresenter deleteDynamicPresenter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_dynamic;
    }

    @Override
    protected void initView() {
        dynamics = new ArrayList<>();
        rv_dynamic_list.setLayoutManager(new LinearLayoutManager(this));
        dynamicListAdapter = new DynamicListAdapter(dynamics, "2");
        rv_dynamic_list.setAdapter(dynamicListAdapter);
        rv_dynamic_list.setEmptyView(empty_view);
        spUtils = new SPUtils(this, SpConstant.USER_TOKEN);
        token = spUtils.getString(SpConstant.USER_TOKEN);
        mid = spUtils.getLong(SpConstant.USER_MID);
        //获取数据
        deleteDynamicPresenter = new DeleteDynamicPresenter(new DeleteCall());
        dynamicPresenter = new MyDynamicPresenter(new OrderCall());
        dynamicListAdapter.setOnItemClickListener((adapter, view, position) -> {
            Dynamic.RecordsDTO item = (Dynamic.RecordsDTO) adapter.getData().get(position);
            Bundle bundle = new Bundle();
            bundle.putSerializable("subjectId", item.getId() + "");
            bundle.putString("status", "0");
            intent(DynamicDetailActivityTwo.class, bundle);
        });
        dynamicListAdapter.addChildClickViewIds(R.id.tv_delate);
        dynamicListAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                if (view.getId() == R.id.tv_delate) {
                    Dynamic.RecordsDTO item = (Dynamic.RecordsDTO) adapter.getData().get(position);
                    String id = item.getId()+"";
                    showDialog(id);
                }
            }
        });
    }

    class DeleteCall implements DataCall<String> {

        @Override
        public void success(String data, Object... args) {
            dynamicPresenter.reqeust(true, mid + "", "Bearer " + token);
            UIUtils.showToastSafe("删除成功");
        }

        @Override
        public void fail(ApiException e, Object... args) {

        }
    }

    private void showDialog(String id) {
        View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_bottom_view, null);
        BottomDialog bottomDialog = new BottomDialog(this);
        bottomDialog.setContentView(dialogView);
        bottomDialog.setCanceledOnTouchOutside(true);
        bottomDialog.show();

        TextView tv_cancel = dialogView.findViewById(R.id.tv_cancel);
        TextView tv_sure = dialogView.findViewById(R.id.tv_sure);
        TextView tv_title = dialogView.findViewById(R.id.tv_title);
        tv_title.setText("确定删除该条动态吗？");
        tv_cancel.setOnClickListener(v -> bottomDialog.dismiss());
        tv_sure.setOnClickListener(v -> {
            deleteDynamicPresenter.reqeust("Bearer " + token, id);
            bottomDialog.dismiss();
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        dynamicPresenter.reqeust(true, mid + "", "Bearer " + token);
    }

    class OrderCall implements DataCall<Dynamic> {

        @Override
        public void success(Dynamic data, Object... args) {
            EventBus.getDefault().post(RefreshResult.getInstance(true, true));
            //添加列表并刷新
            if (dynamicPresenter.getPage() == 1) {
                dynamics.clear();
            }
            dynamicListAdapter.setNewInstance(data.getRecords());
            dynamicListAdapter.notifyDataSetChanged();
        }

        @Override
        public void fail(ApiException e, Object... args) {
            EventBus.getDefault().post(RefreshResult.getInstance(true, true));
        }
    }

    @OnClick(R.id.iv_back)
    public void back() {
        finish();
    }

    @Override
    protected void destoryData() {

    }
}
